package atom.core3.datasource;

import org.apache.commons.codec.binary.Base64;

import atom.core0.util.security.JDES;

import com.alibaba.druid.pool.DruidDataSource;

public class SecretDruidDataSource extends DruidDataSource {
	// des加密 + base64
	public synchronized void setPassword(String password) {
		String newpassword = decryptDB(password);
		super.setPassword(newpassword);
	}
	public synchronized void setUsername(String username) {
		String newusername = decryptDB(username);
		super.setUsername(newusername);
	}

	public static void main(String[] args) throws Exception{
		JDES des = new JDES();
		des.SetKey("antlym98".getBytes());
		String password="abc12";
		byte[] b1 = des.doECBEncrypt(password.getBytes(), password.length());
		byte[] b2 = Base64.encodeBase64(b1);
		String pp = new String(b2);
		System.out.println(pp);
		
		new SecretDruidDataSource().setPassword(pp);
		
	}
	public static String decryptDB(String password)
	{
		byte[] secpassword = Base64.decodeBase64(password);
		JDES des = new JDES();
		//此密钥放到数据库中，读取到内存中，这样的话，破解代码也不知道密钥
		des.SetKey("antlym98".getBytes());
		String newpassword = "";
		try {

			byte[] newbyte = des.doECBDecrypt(secpassword, secpassword.length);
			newpassword = new String(newbyte);
		} catch (Exception e) {
			e.printStackTrace();
			newpassword="";
		}		
		return newpassword;
	}
	
}
